//
//  APiXCameraView.m
//  APiXCameraDemo
//
//  Created by APiX on 16/5/9.
//  Copyright © 2016年 APiX. All rights reserved.
//

#import "APiXCameraView.h"
@implementation APiXCameraView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.avaterImageView];
        [self addSubview:self.nameLabel];
        [self addSubview:self.nationLabel];
        [self addSubview:self.sexLabel];
        [self addSubview:self.brithLabel];
        [self addSubview:self.addressLabel];
        [self addSubview:self.idLabel];
        [self addSubview:self.signAuthorityLabel];
        [self addSubview:self.validLabel];
        [self addSubview:self.fontButton];
        [self addSubview:self.backButton];
        
    }
    return self;
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    NSDictionary *views= NSDictionaryOfVariableBindings(_avaterImageView,_nameLabel,_nationLabel,_sexLabel,_brithLabel,_addressLabel,_idLabel,_signAuthorityLabel,_validLabel,_fontButton,_backButton);
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-12-[_avaterImageView(==with)]-15-[_nameLabel]-15-[_sexLabel]-15-[_addressLabel]-15-[_idLabel]-15-[_signAuthorityLabel]-15-[_validLabel]-20-[_fontButton(40)]" options:NSLayoutFormatAlignAllLeft metrics:@{@"with":@((SCREEN_WITH-24)/1.6)} views:views]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-12-[_avaterImageView]-12-|" options:0 metrics:nil views:views]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-12-[_nameLabel(_nationLabel)][_nationLabel(_nameLabel)]-12-|" options:NSLayoutFormatAlignAllCenterY metrics:nil views:views]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-12-[_sexLabel(_brithLabel)][_brithLabel(_sexLabel)]-12-|" options:NSLayoutFormatAlignAllCenterY metrics:nil views:views]];
    
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-12-[_addressLabel]-12-|" options:0 metrics:nil views:views]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-12-[_idLabel]-12-|" options:0 metrics:nil views:views]];

    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-12-[_signAuthorityLabel]-12-|" options:0 metrics:nil views:views]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-12-[_validLabel]-12-|" options:0 metrics:nil views:views]];
    
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-12-[_fontButton(_backButton)]-30-[_backButton(_fontButton)]-12-|" options:NSLayoutFormatAlignAllBottom|NSLayoutFormatAlignAllTop metrics:nil views:views]];

}

//回调
- (void)action:(id)sender{
    UIButton *button = (UIButton *)sender;
    NSInteger tag = button.tag;
    if (self.actionBlock) {
        self.actionBlock(tag);
    }

}
- (UIImageView *)avaterImageView{
    if (!_avaterImageView) {
        _avaterImageView = [[UIImageView alloc] init];
        _avaterImageView.translatesAutoresizingMaskIntoConstraints = NO;
        _avaterImageView.contentMode = UIViewContentModeScaleAspectFit;
        _avaterImageView.backgroundColor = [UIColor grayColor];
    }
    return _avaterImageView;
    
}

- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.translatesAutoresizingMaskIntoConstraints = NO;
        _nameLabel.font = [UIFont systemFontOfSize:14];
        _nameLabel.text = @"姓名:";
    }
    return _nameLabel;

}

- (UILabel *)nationLabel{
    if (!_nationLabel) {
        _nationLabel = [[UILabel alloc] init];
        _nationLabel.translatesAutoresizingMaskIntoConstraints = NO;
        _nationLabel.font = [UIFont systemFontOfSize:14];
        _nationLabel.text = @"民族:";
    }
    return _nationLabel;
    
}

- (UILabel *)sexLabel{
    if (!_sexLabel) {
        _sexLabel = [[UILabel alloc] init];
        _sexLabel.translatesAutoresizingMaskIntoConstraints = NO;
        _sexLabel.font = [UIFont systemFontOfSize:14];
        _sexLabel.text = @"性别:";
    }
    return _sexLabel;
    
}

- (UILabel *)brithLabel{
    if (!_brithLabel) {
        _brithLabel = [[UILabel alloc] init];
        _brithLabel.translatesAutoresizingMaskIntoConstraints = NO;
        _brithLabel.font = [UIFont systemFontOfSize:14];
        _brithLabel.text = @"出生:";
    }
    return _brithLabel;
    
}

- (UILabel *)addressLabel{
    if (!_addressLabel) {
        _addressLabel = [[UILabel alloc] init];
        _addressLabel.translatesAutoresizingMaskIntoConstraints = NO;
        _addressLabel.font = [UIFont systemFontOfSize:14];
        _addressLabel.numberOfLines = 0;
        _addressLabel.text =@"地址:";
    }
    return _addressLabel;
    
}

- (UILabel *)idLabel{
    if (!_idLabel) {
        _idLabel = [[UILabel alloc] init];
        _idLabel.translatesAutoresizingMaskIntoConstraints = NO;
        _idLabel.font = [UIFont systemFontOfSize:14];
        _idLabel.text = @"身份证号:";
    }
    return _idLabel;
    
}

- (UILabel *)signAuthorityLabel{
    if (!_signAuthorityLabel) {
        _signAuthorityLabel = [[UILabel alloc] init];
        _signAuthorityLabel.translatesAutoresizingMaskIntoConstraints = NO;
        _signAuthorityLabel.font = [UIFont systemFontOfSize:14];
        _signAuthorityLabel.text = @"发证机关:";
    }
    return _signAuthorityLabel;
    
}

- (UILabel *)validLabel{
    if (!_validLabel) {
        _validLabel = [[UILabel alloc] init];
        _validLabel.translatesAutoresizingMaskIntoConstraints = NO;
        _validLabel.font = [UIFont systemFontOfSize:14];
        _validLabel.text = @"有效期:";
    }
    return _validLabel;
    
}

- (UIButton *)fontButton{
    if (!_fontButton) {
        _fontButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _fontButton.translatesAutoresizingMaskIntoConstraints = NO;
        [_fontButton setTitle:@"识别正面" forState:UIControlStateNormal];
        _fontButton.backgroundColor = [UIColor blueColor];
        [_fontButton addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
        _fontButton.tag =  KButtonTypeFont;
       
    }
    return _fontButton;
    
}

- (UIButton *)backButton{
    if (!_backButton) {
        _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _backButton.translatesAutoresizingMaskIntoConstraints = NO;
        [_backButton setTitle:@"识别反面" forState:UIControlStateNormal];
        _backButton.backgroundColor = [UIColor blueColor];
        _backButton.tag =  KButtonTypeBack;
        [_backButton addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backButton;
    
}

@end
